﻿Imports Comercializacion_BE
Imports Comercializacion_BL
Imports System.Windows.Forms

Public Class frm_periodos

    Dim _periodos As New periodosBL
    Dim operiodos As New periodosBE
    Dim nopc As Integer = 0

    Private Sub txtCodigoMetal_TextChanged(sender As System.Object, e As System.EventArgs)

    End Sub

    Private Sub frm_periodos_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        'tsAgregar.Enabled = False
        tsEliminar.Enabled = False
        tsModificar.Enabled = False

        cbAnios.Enabled = False
        cbMeses.Enabled = False
        cbEstados.Enabled = False

        btnGuardar.Enabled = False
        btnCancelar.Enabled = False

        Dim listaAnios As New List(Of aniosBE)
        listaAnios = _periodos.RecuperarAnios()
        cbAnios.DataSource = listaAnios
        cbAnios.DisplayMember = "anio"
        cbAnios.ValueMember = "anio"

        Dim listaMeses As New List(Of mesesBE)
        listaMeses = _periodos.RecuperarMeses()
        cbMeses.DataSource = listaMeses
        cbMeses.DisplayMember = "mes"
        cbMeses.ValueMember = "codigomes"

        Dim listaEstados As New List(Of estadosBE)
        listaEstados = _periodos.RecuperarEstados()
        cbEstados.DataSource = listaEstados
        cbEstados.DisplayMember = "nombreestado"
        cbEstados.ValueMember = "codigoestado"

        LlenarTabla()

    End Sub

    Private Sub LlenarTabla()
        gcMetalesPreciosos.DataSource = _periodos.ObtenerPeriodosGrid()
    End Sub


    Private Sub tsAgregar_Click(sender As System.Object, e As System.EventArgs) Handles tsAgregar.Click
        nopc = 1
        tsAgregar.Enabled = False

        cbAnios.Enabled = True
        cbMeses.Enabled = True
        cbEstados.Enabled = False

        btnGuardar.Enabled = True
        btnCancelar.Enabled = True
    End Sub

    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click

        tsAgregar.Enabled = True
        tsEliminar.Enabled = False
        tsModificar.Enabled = False

        cbAnios.Enabled = False
        cbMeses.Enabled = False
        cbEstados.Enabled = False

        btnGuardar.Enabled = False
        btnCancelar.Enabled = False
    End Sub

    Private Sub tsSalir_Click(sender As System.Object, e As System.EventArgs) Handles tsSalir.Click
        Me.Close()
    End Sub

    Private Sub btnGuardar_Click(sender As System.Object, e As System.EventArgs) Handles btnGuardar.Click

        'Dim per As New periodosBL

        Dim anio As String
        Dim mes As String

        anio = cbAnios.SelectedValue
        mes = cbMeses.SelectedValue

        Dim lista As New List(Of periodosBE)


        Select Case nopc
            Case 1

                lista = _periodos.ValidarPeriodo(anio, mes)

                If lista.Count > 0 Then
                    MessageBox.Show("El periodo que desea guardar ya existe")
                Else

                    operiodos.codigoperiodo = anio + mes
                    operiodos.numeroanio = anio
                    operiodos.numeromes = mes
                    operiodos.creadopor = ""
                    operiodos.fechacreacion = Date.Now()
                    operiodos.estado = cbEstados.SelectedValue

                    _periodos.InsertarPeriodos(operiodos)
                    MessageBox.Show("El periodo se ha guardado con exito")

                    tsAgregar.Enabled = True
                    tsEliminar.Enabled = False
                    tsModificar.Enabled = False

                    cbAnios.Enabled = False
                    cbMeses.Enabled = False
                    cbEstados.Enabled = False

                    btnGuardar.Enabled = False
                    btnCancelar.Enabled = False

                    LlenarTabla()

                End If
            Case 2
                'per.ModificarPeriodos(prds)
                'MessageBox.Show("El periodo ha sido modificado con exito")
                'LlenarTabla()

        End Select






    End Sub

    Private Sub cbAnios_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cbAnios.SelectedIndexChanged

    End Sub

    Private Sub tsModificar_Click(sender As System.Object, e As System.EventArgs) Handles tsModificar.Click
        nopc = 2
        tsAgregar.Enabled = False

        cbAnios.Enabled = False
        cbMeses.Enabled = False
        cbEstados.Enabled = True

        btnGuardar.Enabled = True
        btnCancelar.Enabled = True
    End Sub
End Class